# Maintainer: Alexey Pavlov <Alexpux@gmail.com>
# Contributor: Paul Moore <p.f.moore@gmail.com>

_realname=diffutils
pkgbase=mingw-w64-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-${_realname}")
pkgver=3.6
pkgrel=2
pkgdesc="GNU Diffutils is a package of several programs related to finding differences between files (mingw-w64)"
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64')
url='https://www.gnu.org/software/diffutils/'
license=('GPL3')
depends=("${MINGW_PACKAGE_PREFIX}-libsigsegv"
         "${MINGW_PACKAGE_PREFIX}-libwinpthread"
         "${MINGW_PACKAGE_PREFIX}-gettext")
options=('strip' '!libtool' 'staticlibs')
source=("https://ftp.gnu.org/gnu/${_realname}/${_realname}-${pkgver}.tar.xz"
        "win_use_raise_rather_than_kill.patch"
        "win64_dont_check_ptrdiff_size.patch"
        "msvc_invalid_parameter_handler.patch"
        "win_signal_handling.patch"
        )
sha256sums=('d621e8bdd4b573918c8145f7ae61817d1be9deb4c8d2328a65cea8e11d783bd6'
            'f84cc3a08e9c6aff722629137e95c66f4ef9bcb5121735c091bebc705082aacd'
            '96c1e884f25441b91b46ff98853afdb19549228c50036bc86c35974ef3a272f2'
            '8842a1acaf127087ae2c52ed1c7da9275c9ba8aae02ce0df7dd59dbac89b16d9'
            '8b3b36077f45e08ef7ff34c700771ef35d4398613d05c3e80939c1eb0055ce4d')


prepare() {
  cd ${srcdir}/${_realname}-${pkgver}
  patch -p0 -i ${srcdir}/win_use_raise_rather_than_kill.patch
  patch -p0 -i ${srcdir}/msvc_invalid_parameter_handler.patch
  patch -p0 -i ${srcdir}/win_signal_handling.patch

  #make test cases work
  sed -i "s|case \$perms in drwx--\[-S\]---\*|case \$perms in drwx\*|" tests/init.sh
}

build() {
  [[ -d "build-${MINGW_CHOST}" ]] && rm -rf "build-${MINGW_CHOST}"
  mkdir -p "${srcdir}/build-${MINGW_CHOST}"
  cd "${srcdir}/build-${MINGW_CHOST}"

  ../${_realname}-${pkgver}/configure --prefix=${MINGW_PREFIX} \
    --build=${MINGW_CHOST} \
    --host=${MINGW_CHOST} \
    --target=${MINGW_CHOST}

  make WINDOWS_STAT_INODES=1
}

package() {
  cd ${srcdir}/build-${MINGW_CHOST}
  make install DESTDIR="${pkgdir}"

  # Licenses
  install -Dm644 "${srcdir}/${_realname}-${pkgver}/README" "${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/README"
  install -Dm644 "${srcdir}/${_realname}-${pkgver}/COPYING" "${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/COPYING"
}
